package com.example.car

import android.content.Intent
import android.os.Bundle
import android.view.Window
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.example.car.bean.TitleInfo
import com.example.car.databinding.ActivityNewsBinding
import com.example.car.db.SubDbHelper
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator

class NewsActivity : AppCompatActivity() {
    lateinit var binding: ActivityNewsBinding
    val subDbHelper = SubDbHelper.getInstance(this)

    lateinit var titleInfos: List<TitleInfo>


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityNewsBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val window: Window = this@NewsActivity.window
        window.statusBarColor = ContextCompat.getColor(this@NewsActivity, R.color.black)


        // 新增数据
        addData()
        titleInfos = subDbHelper.queryMultipleData(1)
        bindData()
    }


    // 当 Activity 切换到前台并获得焦点时调用
    override fun onResume() {
        super.onResume()
        titleInfos = subDbHelper.queryMultipleData(1)
        bindData()
    }

    fun bindData() {
        binding.apply {
            // 顶部导航栏点击事件
            tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
                override fun onTabSelected(tab: TabLayout.Tab) {
                    viewPager.currentItem = tab.position
                }

                override fun onTabUnselected(tab: TabLayout.Tab?) {
                }

                override fun onTabReselected(tab: TabLayout.Tab?) {
                }

            })
            // 熏染展示界面
            viewPager.adapter = object : FragmentStateAdapter(this@NewsActivity) {
                override fun createFragment(position: Int): Fragment {
                    // 滑动到对应位置时创建Fragment实例
                    val title_py = titleInfos[position].title_py!!
                    return TabNewsFragment.newInstance(title_py)
                }

                override fun getItemCount(): Int {
                    return titleInfos.size
                }
            }
            // 关联标题与展示界面
            TabLayoutMediator(tabLayout, viewPager) { tab, position ->
                // 设置顶部导航栏标题
                tab.text = titleInfos[position].title
            }.attach()

            // 订阅按钮
            btnAdd.setOnClickListener {
                val intent = Intent(this@NewsActivity, SubChannelActivity::class.java)
                intent.putParcelableArrayListExtra(
                    "list",
                    ArrayList(subDbHelper.queryMultipleData())
                )
                startActivity(intent)
            }

        }

    }

    fun addData() {
        // 添加默认数据，并查询已订阅数据
        val _titles = mutableListOf<TitleInfo>()
        _titles.add(TitleInfo(title = "推荐", title_py = "top", is_sub = 1))
        _titles.add(TitleInfo(title = "国内", title_py = "guonei", is_sub = 1))
        _titles.add(TitleInfo(title = "国际", title_py = "guoji"))
        _titles.add(TitleInfo(title = "娱乐", title_py = "yule"))
        _titles.add(TitleInfo(title = "体育", title_py = "tiyu"))
        _titles.add(TitleInfo(title = "军事", title_py = "junshi"))
        _titles.add(TitleInfo(title = "科技", title_py = "keji"))
        _titles.add(TitleInfo(title = "财经", title_py = "caijing"))
        _titles.add(TitleInfo(title = "游戏", title_py = "youxi"))
        _titles.add(TitleInfo(title = "汽车", title_py = "qiche"))
        _titles.add(TitleInfo(title = "健康", title_py = "jiangkang"))
        subDbHelper.insertMultipleData(_titles)
    }
}

